// pages/red_envelopes/Send_red_packets.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide_red_bao:true,
    get_red_bag_info:null,
    send_red_bag_:null,
    aaaa:null,
    sndata:''
  },
  add_index_page() {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },
  //领取红包
  add_money() {
    let that = this;
    let k = wx.getStorageSync('key');
    let sn = that.data.send_red_bag_.sn;
    let pass = that.data.send_red_bag_.password;
    console.log('sn===>' + sn, 'pass====>' + pass);

    // wx.showToast({
    //   title: sn,
    //   icon: 'success',
    //   duration: 1000
    // })
    wx.request({
      url: ApiUrl + "/index.php?act=red_bag&op=get_red_bag_info",
      data: { 
        key: k,
        sn:sn,
        password:pass
      },
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'post',
      success: function (result) {
        console.log(result);
        let res = result.data.datas.data;
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            // console.log(result.data.datas);
            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 1000
            })
            that.setData({
              sndata: res.sn,
              hide_red_bao:false
            });
            
            that.red_bag_cont();

          }else{
            wx.showToast({
              title: '已经被领取',
              icon: 'success',
              duration: 1000
            })
          }
        }
      }
    })
  },
  // aaa(){
  //   this.setData({
  //     hide_red_bao:false
  //   });
  // },
  red_bag_cont() {
  //index.php?act=red_bag&op=voucher_list
    let k = wx.getStorageSync('key');
    let that = this;
    wx.showToast({
      title: 'xxxx=>' + that.data.sndata,
      icon: 'success',
      duration: 1000
    })
    let sns = that.data.sndata;
    wx.request({
      url: ApiUrl + "/index.php?act=red_bag&op=voucher_list",
      data: {
        key: k,
        sn: that.data.sndata
      },
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'post',
      success: function (result) {
        console.log(result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.data;
            console.log(result.data.datas);
            that.setData({
              get_red_bag_info: res,
              hide_red_bao: false
            });
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //send_red_bag_datas=' + send_red_bag_datas

    if (options.send_red_bag_datas){

      let send_red_bag_datas = JSON.parse(options.send_red_bag_datas);

      this.setData({
        send_red_bag_: send_red_bag_datas,
        // aaaa: options.send_red_bag_datas
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})